# backend/api/v1/nodes.py - 修复后的完整版本
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession
from backend.database import get_db
from backend.schemas import node as node_schemas
from backend.crud import node as crud
from backend.api.deps import get_current_user  # 添加认证依赖

router = APIRouter(prefix="/nodes", tags=["nodes"])

@router.post("/", response_model=node_schemas.NodeOut)
async def create_node(
    data: node_schemas.NodeCreate,
    db: AsyncSession = Depends(get_db),
    current_user: node_schemas.NodeOut = Depends(get_current_user)  # 添加认证
):
    return await crud.create_node(db, data)

# 添加无斜杠版本的路由，避免307重定向
@router.get("", response_model=list[node_schemas.NodeOut])
async def read_nodes_no_slash(
    skip: int = 0,
    limit: int = 100,
    db: AsyncSession = Depends(get_db),
    current_user: node_schemas.NodeOut = Depends(get_current_user)  # 添加认证
):
    """
    获取节点列表（无斜杠版本）
    """
    return await crud.get_nodes(db, skip, limit)

@router.get("/", response_model=list[node_schemas.NodeOut])
async def read_nodes(
    skip: int = 0,
    limit: int = 100,
    db: AsyncSession = Depends(get_db),
    current_user: node_schemas.NodeOut = Depends(get_current_user)  # 添加认证
):
    """
    获取节点列表（有斜杠版本）
    """
    return await crud.get_nodes(db, skip, limit)

# 可选：添加获取单个节点的接口
@router.get("/{node_id}", response_model=node_schemas.NodeOut)
async def read_node(
    node_id: str,
    db: AsyncSession = Depends(get_db),
    current_user: node_schemas.NodeOut = Depends(get_current_user)
):
    """
    根据ID获取节点信息
    """
    node = await crud.get_node(db, node_id)
    if not node:
        raise HTTPException(status_code=404, detail="Node not found")
    return node